package com.lsmo.android.carfans.activity.car;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.model.car.BrandTypeThread;
import com.lsmo.android.carfans.model.car.CarSummaryThread;
import com.lsmo.android.carfans.process.car.BrandTypeProcess;
import com.lsmo.android.carfans.view.car.SelectCarView;
import com.lsmo.android.common.base.BaseActivity;
import com.lsmo.android.common.base.BaseProcess;
import com.lsmo.android.common.base.BaseTask;
import com.lsmo.android.common.base.ViewProcessCallBack;

public class CarSummaryActivity extends BaseActivity 
	implements AdapterView.OnItemClickListener, View.OnClickListener,
		ViewProcessCallBack {

	public static final String TAG = SelectCarView.class.getSimpleName();
	private ArrayList<CarSummaryThread> arrayList;
	private ArrayList<BrandTypeThread> brandTypeThread;
	private HashMap<String, ArrayList<CarSummaryThread>> hashMap;
	private ListView mListView;
	
	private ViewGroup mProgressBarLayout;
	private BrandTypeProcess bTypeProcess;
	private String[] params = null;
	public static int carid;
	public static String title;
	public Context mContext;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.bTypeProcess = new BrandTypeProcess();
		this.mContext = CarSummaryActivity.this;
		
		carid = getIntent().getIntExtra("carid", 0);
		title = getIntent().getStringExtra("title");
		
		initView();//加载view
		
		updateView();//加载页面数据
	}
	
	public void initView(){
		this.setContentView(R.layout.car_view_only_list);
		mProgressBarLayout = (ViewGroup)this.findViewById(R.id.loading_layout);
		mListView = (ListView)this.findViewById(R.id.listView);
		mListView.setOnItemClickListener(this);//注册listItem监听
	}

	/**
	 * lisview监听事件
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
	    int ids = brandTypeThread.get(arg2).getID();
	    String name = brandTypeThread.get(arg2).getName();
	    String carName = brandTypeThread.get(arg2).getModelName();
		CarTypeActivity.launch(mContext, arg2, ids, name, carName);
	}

	/**
	 * 按钮监听
	 */
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
	}

	@Override
	public void processAfterTaskFinish(int code, Object result) {
		// TODO Auto-generated method stub
		if(result !=null){
			refreshView();
		}
		this.mProgressBarLayout.setVisibility(View.GONE);
	}

	@Override
	public void updateView() {
		// TODO Auto-generated method stub
		this.mProgressBarLayout.setVisibility(View.GONE);
		
			new CarSummaryAsyncTask(CarSummaryAsyncTask.GET_CAR_SUMMARY_LIST,
				this.mContext,
				this,
				this.bTypeProcess 
		).execute(new String[]{});
	}
	
	private class CarSummaryAsyncTask extends BaseTask{

 		public static final int GET_CAR_SUMMARY_LIST =6;
		private BrandTypeProcess bTypeProcess ;
		
		public CarSummaryAsyncTask(int code , Context context,ViewProcessCallBack view,BaseProcess process ) {
			super(code,context,view );
			this.bTypeProcess = (BrandTypeProcess) process;
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		
		@Override
		protected Object doInBackground(String... params) {
			super.doInBackground(params);
			Object result = null;
	 
			switch(this.mCode){
			    
				case GET_CAR_SUMMARY_LIST:
					
					//加载数据
					result = bTypeProcess.getBrandTypeThreadList(params, carid);
					brandTypeThread = (ArrayList<BrandTypeThread>) result;
					
					break;
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			switch(this.mCode){
				 
				case GET_CAR_SUMMARY_LIST:
					
				 		mView.processAfterTaskFinish(this.mCode ,result );
				 	
				 	break;
		   }
		}
		
	}
	
	/*
	 * 刷新页面数据
	 */
	public void refreshView(){
		if(brandTypeThread !=null){
			BrandTypeAdapter aTypeAdapter = new BrandTypeAdapter(this);
			aTypeAdapter.setList(brandTypeThread);
			// separate adapters on one list
			SeparatedListAdapter separatedAdapter = new SeparatedListAdapter(this);
			separatedAdapter.addSection(brandTypeThread.get(0).getShowName(), aTypeAdapter);
			
			mListView.setAdapter(separatedAdapter);
		}
	}
	
}
